package com.example.hengjun.operation_oracle.logback;

import com.example.hengjun.operation_oracle.controller.UserController;
import com.example.hengjun.operation_oracle.entity.Testuser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("log")
public class TestLogBack {
    private final static Logger logger = LoggerFactory.getLogger(UserController.class);

    @GetMapping("test")
    public void myTest(){
    //下列日志输出为学习logback
    Object entry = new Testuser();
        logger.info("The entry is " + entry);
        logger.debug("记录debug日志");
        logger.info("访问了find方法");
        logger.error("记录错误日志");


    //日志输出代码优化
    //1.普通写法
    Object entry1 = new Testuser();
        logger.debug("The entry is " + entry1);

    //2.优化写法
        if(logger.isDebugEnabled()) {
        Object entry2 = new Testuser();
        logger.debug("The entry is " + entry2);
    }

    //3.占位符写法, 只有在评估是否记录之后，并且只有在决策是肯定的情况下，记录器实现才会格式化消息并将“{}”对替换为条目的字符串值。 换句话说，当禁用日志语句时，此表单不会产生参数构造的成本。
    Object entry3 = new Testuser();
        logger.debug("The entry is {}.", entry3);
        logger.debug("The new entry is {}. It replaces {}.", entry3, entry1);
        Object[] paramArray = {entry1  , entry3};
        logger.debug("Value {} was inserted between {} and {}.", paramArray);

    //4.,logger.error(e) 不会打印出堆栈信息，打印堆栈信息 正确的写法是：
//        logger.error("程序异常, 详细信息:{}", e.getLocalizedMessage() , e);
    }

}
